using System;
using System.Collections.Generic;
using ClubFit.DataAccess;
using ClubFit.Domain;
using NHibernate;
using NHibernate.Cfg;

namespace ClubFit.DataAccess
{
    public class DatabaseManager 
    {
        private Configuration configuration;
        private ISessionFactory factory;

        public DatabaseManager()
        {
            configuration = new Configuration();
            configuration.Configure();
            configuration.AddClass(typeof (Meals));
            //configuration.AddClass(typeof (Goals));
            //configuration.AddClass(typeof (Exercise));
            //configuration.AddClass(typeof (ExerciseType));
            //configuration.AddClass(typeof (User));
            factory = configuration.BuildSessionFactory();
        }

        public object[] GetAll()
        {
            ISession session = null;
            ITransaction transaction = null;
            List<Meals> manyMeals = new List<Meals>();
            try
            {
                session = factory.OpenSession();
                transaction = session.BeginTransaction();
                
                manyMeals.AddRange(session.CreateCriteria(typeof (Meals)).List<Meals>());
                transaction.Commit();
                session.Close();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                session.Close();
            }

            return manyMeals.ToArray();
        }

        public void Save(object[] items)
        {
            ISession session = null;
            ITransaction transaction = null;
            

            try
            {
                session = factory.OpenSession();
                transaction = session.BeginTransaction();

                foreach (object item in items)
                    session.SaveOrUpdate(item);    

                transaction.Commit();
                session.Close();
                
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                session.Close();
            }
        }

        public void Update(object item)
        {
            ISession session = null;
            ITransaction transaction = null;


            try
            {
                session = factory.OpenSession();
                transaction = session.BeginTransaction();
              
                session.SaveOrUpdate(item);

                transaction.Commit();
                session.Close();

            }
            catch (Exception ex)
            {
                transaction.Rollback();
                session.Close();
            }
        }

        public void Delete(object obj)
        {
            ISession session = null;
            ITransaction transaction = null;
            

            try
            {
                session = factory.OpenSession();
                transaction = session.BeginTransaction();
                session.Delete(obj);
                transaction.Commit();
                session.Close();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                session.Close();
            }

        }

        public object Get(int id)
        {
            ISession session = null;
            ITransaction transaction = null;
            object obj = null;
            Meals meals = null;

            try
            {
                session = factory.OpenSession();
                transaction = session.BeginTransaction();
                meals = (Meals) session.Load(typeof(Meals), id);
                
                transaction.Commit();
                session.Close();


            }
            catch (Exception ex)
            {
                transaction.Rollback();
                session.Close();
            }

            return meals;
        }
    }
}